package cn.abyss.admin.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * CHDFC日期工具类
 * @author: hc
 * @date: 2019/05/28 20:42
 */
public class AbyssAdminDateUtil {
	private AbyssAdminDateUtil(){}
	
	private static Map<String, SimpleDateFormat> dataFormatMap = new HashMap<>();
	
	/**
	 * 返回不带时分秒的当天日期
	 * @return
	 */
	public static Date getToday(){
		return getTodayCalendar().getTime();
	}
	/**
	 * 返回不带时分秒的日期
	 * @param date
	 * @return
	 */
	public static Date getDatePart(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		clearCalendarTime(calendar);
		return calendar.getTime();
	}
	/**
	 * 获取指定日期月份的第一天
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfMonth(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}
	/**
	 * 获取指定日期月份的最后一天
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfMonth(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return calendar.getTime();
	}
	/**
	 * 获取指定日期周的第一天
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfWeek(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int d = 0;
		if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
			d = -6;
		}else{
			d = Calendar.MONDAY - calendar.get(Calendar.DAY_OF_WEEK);
		}
		calendar.add(Calendar.DAY_OF_WEEK, d);
		return calendar.getTime();
	}
	/**
	 * 获取指定日期周的最后一天
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfWeek(Date date){
		Calendar calendar = Calendar.getInstance();
		Date monday = getFirstDayOfWeek(date);
		calendar.setTime(monday);
		calendar.add(Calendar.DAY_OF_WEEK, 6);
		return calendar.getTime();
	}
	/**
	 * 获取与今天距离间隔天数的日期
	 * @param interval
	 * @return
	 */
	public static Date getByIntervalDay(int interval){
		Calendar calendar = getTodayCalendar();
		calendar.add(Calendar.DATE, interval);
		return calendar.getTime();
	}
	/**
	 * 获取与当月距离间隔月份的日期（第一天）
	 * @param interval
	 * @return
	 */
	public static Date getByIntervalMonth(int interval){
		Calendar calendar = getTodayCalendar();
		calendar.add(Calendar.MONTH, interval);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}
	/**
	 * 返回今天是本月第几天
	 * @return
	 */
	public static int getIntervalDaysOfMonthForToday(){
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.DAY_OF_MONTH);
	}
	/**
	 * 返回指定日期在本月第几天
	 * @return
	 */
	public static int getIntervalDaysOfMonth(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}
	/**
	 * 返回2个日期的间隔天数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int getIntervalDays(Date date1, Date date2){
		Date d1 = getDatePart(date1);
		Date d2 = getDatePart(date2);
		return (int)((d2.getTime() - d1.getTime()) / (1L*1000*3600*24));
	}
	/**
	 * 返回2个日期中最大的日期
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static Date getMaxDate(Date date1, Date date2){
		int daysDiff = getIntervalDays(date1, date2);
		if(daysDiff >= 0){
			return date2;
		}
		return date1;
	}
	/**
	 * 返回2个日期中最小的日期
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static Date getMinDate(Date date1, Date date2){
		int daysDiff = getIntervalDays(date1, date2);
		if(daysDiff >= 0){
			return date1;
		}
		return date2;
	}
	/**
	 * 返回指定日期与今天的间隔天数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int getIntervalDaysFromToday(Date date){
		Date d = getDatePart(date);
		Date today = getToday();
		return (int)((d.getTime() - today.getTime()) / (1L*1000*3600*24));
	}
	/**
	 * 返回指定日期+间隔天数的日期
	 * @param date
	 * @param interval
	 * @return
	 */
	public static Date addDays(Date date, int interval){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, interval);
		return calendar.getTime();
	}
	
	/**
	 * 根据格式解释日期
	 * @param source
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String source, String pattern) throws ParseException{
		return getDateFormat(pattern).parse(source);
	}
	/**
	 * 格式化日期
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String format(Date date, String pattern) {
		return getDateFormat(pattern).format(date);
	}
	
	// 获取当天的日历
	private static Calendar getTodayCalendar(){
		Calendar calendar = Calendar.getInstance();
		clearCalendarTime(calendar);
		return calendar;
	}
	// 清空时间的值
	private static void clearCalendarTime(Calendar calendar){
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
	}
	// 获取格式化类
	private static SimpleDateFormat getDateFormat(String pattern){
		if(!dataFormatMap.containsKey(pattern)){
			SimpleDateFormat dataFormat = new SimpleDateFormat(pattern);
			dataFormatMap.put(pattern, dataFormat);
		}
		return dataFormatMap.get(pattern);
	}
}
